<?php
/**
 * Created by PhpStorm.
 * User: LuLingFeng
 * Date: 2018/1/19
 * Time: 14:44
 */
namespace App\Http\Admin\Personnel\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Oa\AttendanceLog as EloquentLog;

class AttendanceLog extends Model
{
    const LOG_TYPE_CLOCK_IN = 1;
    const LOG_TYPE_CLOCK_OUT = 2;
    const LOG_TYPE_PERSONAL_LEAVE = 3;
    const LOG_TYPE_MEDICAL_LEAVE = 4;
    const LOG_RESULT_TYPE_CLOCK_IN = [1, 2, 3];
    const LOG_RESULT_TYPE_CLOCK_OUT = [4, 5];
    const LOG_RESULT_TYPE_CLOCK_IN_ON_TIME = 1;
    const LOG_RESULT_TYPE_CLOCK_IN_LATE = 2;
    const LOG_RESULT_TYPE_CLOCK_IN_ABSENCE = 3;
    const LOG_RESULT_TYPE_CLOCK_OUT_EARLY = 4;
    const LOG_RESULT_TYPE_CLOCK_OUT_ON_TIME = 5;

    const LOG_CHECK_IN_TYPE_NORMAL = 1; // 正常打卡
    const LOG_CHECK_IN_TYPE_FIELD = 2;  // 外勤
    const LOG_CHECK_IN_TYPE_PATCH = 3;  // 补签

    public function logList($where, $paging, $select = ['*'], $relation = [])
    {
        $stat = EloquentLog::with($relation)
            ->orderBy('log_time', 'asc');
        // 筛选
        if (!empty($where['oa_contacts_id'])) $stat->where('oa_contacts_id', '=', $where['oa_contacts_id']);
        if (!empty($where['oa_department_id'])) $stat->where('oa_department_id', '=', $where['oa_department_id']);
        if (!empty($where['oa_company_id'])) $stat->where('oa_company_id', '=', $where['oa_company_id']);
        if (!empty($where['relate_id'])) $stat->where('relate_id', '=', $where['relate_id']);
//        if (isset($where['type']) && is_numeric($where['type']))
        if (!empty($where['type']))
            $stat->where('type', '=', $where['type']);
//        if (isset($where['result_type']) && is_numeric($where['result_type']))
        if (!empty($where['result_type']))
            $stat->where('result_type', '=', $where['result_type']);
        if (!empty($where['check_in_type']))
            $stat->where('check_in_type', '=', $where['check_in_type']);
        if (!empty($where['start_time'])) {
            $time = is_numeric($where['start_time']) ? $where['start_time'] : strtotime($where['start_time']);
            $stat->where('log_time', '>=', $time);
        }
        if (!empty($where['end_time'])) {
            $time = is_numeric($where['end_time']) ? $where['end_time'] : strtotime($where['end_time']);
            $stat->where('log_time', '<=', $time);
        }

        $totalRow = $stat->count();
        // 分页
        if (!empty($paging['page']) && !empty($paging['page_size'])) {
            $stat->forPage($paging['page'], $paging['page_size']);
        }

        $list = $stat->get($select);
        return [
            'list'        => $list,
            'totalRow'    => $totalRow,
            'pageCurrent' => isset($paging['page']) ? $paging['page'] : 0
        ];
    }

    public static function getCheckInType($type)
    {
        switch ($type) {
            case self::LOG_CHECK_IN_TYPE_NORMAL:
                $str = '正常打卡';
                break;
            case self::LOG_CHECK_IN_TYPE_FIELD:
                $str = '外勤打卡';
                break;
            case self::LOG_CHECK_IN_TYPE_PATCH:
                $str = '补签';
                break;
            default:
                $str = '未知';
                break;
        }
        return $str;
    }
}